<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.1.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.11.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="Windows屏幕分辨率与DPI的获取 DPI对屏幕分辨率的影响现在屏幕分辨率越来越高，但是很多软件并没有对高分辨率进行适配，所以在高分辨率之下的界面会显示的很小，很难分辨。这个时候开启DPI功能，也就是缩放与布局，可以改善这个情况。  不受DPI影响1、不受DPI影响，获取的实际分辨率信息–主屏分辨率 12345HDC hdc &#x3D; GetDC(NULL);int firstScreenWidth">
<meta property="og:type" content="article">
<meta property="og:title" content="Windows屏幕分辨率与DPI的获取">
<meta property="og:url" content="http://example.com/2020/11/13/Windows%E5%B1%8F%E5%B9%95%E5%88%86%E8%BE%A8%E7%8E%87%E4%B8%8EDPI%E7%9A%84%E8%8E%B7%E5%8F%96/index.html">
<meta property="og:site_name" content="Youan">
<meta property="og:description" content="Windows屏幕分辨率与DPI的获取 DPI对屏幕分辨率的影响现在屏幕分辨率越来越高，但是很多软件并没有对高分辨率进行适配，所以在高分辨率之下的界面会显示的很小，很难分辨。这个时候开启DPI功能，也就是缩放与布局，可以改善这个情况。  不受DPI影响1、不受DPI影响，获取的实际分辨率信息–主屏分辨率 12345HDC hdc &#x3D; GetDC(NULL);int firstScreenWidth">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-11-12T16:00:00.000Z">
<meta property="article:modified_time" content="2022-12-06T08:49:15.672Z">
<meta property="article:author" content="lee">
<meta property="article:tag" content="windows">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2020/11/13/Windows%E5%B1%8F%E5%B9%95%E5%88%86%E8%BE%A8%E7%8E%87%E4%B8%8EDPI%E7%9A%84%E8%8E%B7%E5%8F%96/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2020/11/13/Windows%E5%B1%8F%E5%B9%95%E5%88%86%E8%BE%A8%E7%8E%87%E4%B8%8EDPI%E7%9A%84%E8%8E%B7%E5%8F%96/","path":"2020/11/13/Windows屏幕分辨率与DPI的获取/","title":"Windows屏幕分辨率与DPI的获取"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Windows屏幕分辨率与DPI的获取 | Youan</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Youan</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">好记性不如烂笔头</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Windows%E5%B1%8F%E5%B9%95%E5%88%86%E8%BE%A8%E7%8E%87%E4%B8%8EDPI%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="nav-number">1.</span> <span class="nav-text">Windows屏幕分辨率与DPI的获取</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%8D%E5%8F%97DPI%E5%BD%B1%E5%93%8D"><span class="nav-number">1.1.</span> <span class="nav-text">不受DPI影响</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%97DPI%E5%BD%B1%E5%93%8D"><span class="nav-number">1.2.</span> <span class="nav-text">受DPI影响</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DPI%E7%9A%84%E8%AE%A1%E7%AE%97"><span class="nav-number">1.3.</span> <span class="nav-text">DPI的计算</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B4%E4%BD%93%E6%A2%B3%E7%90%86%EF%BC%9A"><span class="nav-number">1.4.</span> <span class="nav-text">整体梳理：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B3%A8%E6%84%8F%EF%BC%9A"><span class="nav-number">1.5.</span> <span class="nav-text">注意：</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">lee</p>
  <div class="site-description" itemprop="description">记录备份</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">10</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wslmh" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wslmh" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wslmh@outlook.com" title="E-Mail → mailto:wslmh@outlook.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/11/13/Windows%E5%B1%8F%E5%B9%95%E5%88%86%E8%BE%A8%E7%8E%87%E4%B8%8EDPI%E7%9A%84%E8%8E%B7%E5%8F%96/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="lee">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Youan">
      <meta itemprop="description" content="记录备份">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Windows屏幕分辨率与DPI的获取 | Youan">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Windows屏幕分辨率与DPI的获取
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-11-13 00:00:00" itemprop="dateCreated datePublished" datetime="2020-11-13T00:00:00+08:00">2020-11-13</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-12-06 16:49:15" itemprop="dateModified" datetime="2022-12-06T16:49:15+08:00">2022-12-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/windows/" itemprop="url" rel="index"><span itemprop="name">windows</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Windows屏幕分辨率与DPI的获取"><a href="#Windows屏幕分辨率与DPI的获取" class="headerlink" title="Windows屏幕分辨率与DPI的获取"></a>Windows屏幕分辨率与DPI的获取</h1><ul>
<li>DPI对屏幕分辨率的影响<br>现在屏幕分辨率越来越高，但是很多软件并没有对高分辨率进行适配，所以在高分辨率之下的界面会显示的很小，很难分辨。这个时候开启DPI功能，也就是缩放与布局，可以改善这个情况。</li>
</ul>
<h2 id="不受DPI影响"><a href="#不受DPI影响" class="headerlink" title="不受DPI影响"></a>不受DPI影响</h2><p>1、不受DPI影响，获取的实际分辨率信息–主屏分辨率</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">HDC hdc = GetDC(NULL);</span><br><span class="line">int firstScreenWidth, hfirstScreenHeight;</span><br><span class="line">firstScreenWidth = GetDeviceCaps(hdc, DESKTOPHORZRES);</span><br><span class="line">hfirstScreenHeight = GetDeviceCaps(hdc, DESKTOPVERTRES);</span><br><span class="line">ReleaseDC(NULL, hdc);</span><br></pre></td></tr></table></figure>

<p>2、实际分辨率（物理显示器分辨率，不受DPI影响）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">// 获取监视器物理宽度与高度</span><br><span class="line">DEVMODE dm;</span><br><span class="line">dm.dmSize = sizeof(dm);</span><br><span class="line">dm.dmDriverExtra = 0;</span><br><span class="line">EnumDisplaySettings(monitor_info.szDevice, ENUM_CURRENT_SETTINGS, &amp;dm);</span><br><span class="line">int cxPhysical = dm.dmPelsWidth;</span><br><span class="line">int cyPhysical = dm.dmPelsHeight;</span><br></pre></td></tr></table></figure>


<span id="more"></span>



<h2 id="受DPI影响"><a href="#受DPI影响" class="headerlink" title="受DPI影响"></a>受DPI影响</h2><p>例如原分辨率为1920<em>1080，设置DPI为125%的缩放后，得到的结果为1536</em>864，如果缩放为100%的话，那得到的结果就是1920*1080</p>
<p>1、多屏虚拟分辨率，受DPI影响</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int aScreenWidth, aScreenHeight;</span><br><span class="line">aScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);</span><br><span class="line">aScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);</span><br></pre></td></tr></table></figure>

<p>2、显示屏信息，受DPI影响</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">MONITORINFOEX monitor_info;</span><br><span class="line">monitor_info.cbSize = sizeof(MONITORINFOEX);</span><br><span class="line">if(!::GetMonitorInfo(hMo, &amp;monitor_info))</span><br><span class="line"></span><br><span class="line">int cxLogical = (monitor_info.rcMonitor.right - monitor_info.rcMonitor.left);</span><br><span class="line">int cyLogical = (monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top);</span><br></pre></td></tr></table></figure>

<p>3、主屏分辨率</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">RECT deskRect;</span><br><span class="line">GetWindowRect(GetDesktopWindow(), &amp;deskRect);</span><br></pre></td></tr></table></figure>

<p>4、主屏分辨率</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">int allX = GetSystemMetrics(SM_CXSCREEN);</span><br><span class="line">int allY = GetSystemMetrics(SM_CYSCREEM);</span><br></pre></td></tr></table></figure>


<h2 id="DPI的计算"><a href="#DPI的计算" class="headerlink" title="DPI的计算"></a>DPI的计算</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// 缩放比例计算  实际上使用任何一个即可</span><br><span class="line">double horzScale = ((double)cxPhysical / (double)cxLogical);</span><br><span class="line">double vertScale = ((double)cyPhysical / (double)cyLogical);</span><br><span class="line">assert(horzScale == vertScale); // 宽或高这个缩放值应该是相等的</span><br></pre></td></tr></table></figure>

<h2 id="整体梳理："><a href="#整体梳理：" class="headerlink" title="整体梳理："></a>整体梳理：</h2><p>获得显示器逻辑宽高、物理宽高   二者之比 就是DPI<br>如果是次屏，则调整次屏的LEFT值为主屏物理宽度</p>
<h2 id="注意："><a href="#注意：" class="headerlink" title="注意："></a>注意：</h2><p>win10 改变DPI 是立即生效的<br>win7 是注销后生效</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/windows/" rel="tag"># windows</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2020/01/10/Duilib-%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C/" rel="prev" title="duilib 控件文字自动换行">
                  <i class="fa fa-chevron-left"></i> duilib 控件文字自动换行
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2020/11/16/Git-%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/" rel="next" title="Git学习记录">
                  Git学习记录 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lee</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
